(function($){
	function formResult(msg, state) {
        layer.closeAll();
        if(state == 0) {
            layer.alert(msg, {icon: 5});
        }else{
        	location.reload(true);
        }
    }

	function checkparam(){
		var val = $('input[name="card_id"]').val();
		var member_id = parent.getMember_id(),
			finance_type = 1,     //储值卡
			consume_name = $('input[name="consume_name"]').val(),
			sell_price = $('input[name="balance"]').val(),
			really_price = $('input[name="balance"]').val(),
			count = 1,
			give_balance = $('input[name="give_balance"]').val(),
			relation_id = $('input[name="card_id"]').val();

		if(val==null || val==''){
			layer.alert('请选择卡项！', {icon: 5});
			return false;
		} else if (typeof member_id == 'undefined') {
			layer.alert('请选择会员！', {icon: 5});
			return false;
		} else {
			$.ajax({
				type:'POST',
				dataType:'json',
				url:'/business/financial/cashier?action=chooseproduct',
				data:{member_id:member_id,finance_type:finance_type,consume_name:consume_name,sell_price:sell_price,really_price:really_price,count:count,give_balance:give_balance,relation_id:relation_id},
				success:function (data){
					if(data.state==1){
						$.ajax({
							type:'POST',
							dataType:'json',
							url:'/business/financial/cashier?action=productlist',
							data:{member_id:member_id},
							success:function (data){

								if(data.state==1){
									var index = parent.layer.getFrameIndex(window.name);
									var html = '';
									function trans(data) {
										var $type;
										switch (data) {
											case '1':
												$type = '储值卡';
												break;
											case '2':
												$type = '疗程卡';
												break;
											case '3':
												$type = '家居产品';
												break;
											case '4':
												$type = '合作项目';
												break;
											case '5':
												$type = '服务项目';
												break;
										}
										return $type;
									}

									var arrSum = [];
									parent.$('.table tbody').empty();
									$.each(data.result,function(i,o){
										arrSum.push(o.really_price * o.count);
									   html = '<tr>' +
												   '<form>' +
													   '<td class="types"><i>'+ trans(o.finance_type)+'</i><span>充值金额:'+ o.recharge+'</span><span>赠送金额:'+ o.give_balance+'</span></td>' +
													   '<td>'+ o.consume_name+'</td>' +
													   '<td>'+ o.sell_price+'</td>' +
													   '<td><input class="calc reaPrice" type="text" value="'+o.really_price+'"/></td>' +
													   '<td><input class="calc count" type="text" value="'+ o.count +'"></td>' +
													   '<td>'+ o.really_price * o.count+'</td>' +
										   '<td><a data-nid="'+i+'" class="add-member btn btn-sm" href="javascript:;" onclick="openP('+"'/business/financial/cashier?action=stafflist'"+','+ i +','+ o.finance_id+')">'+parent.defaultVal(o.employee_name)+'</a></td>' +
													   '<td><input class="saleFeat" type="text" value="'+ parent.defaultReward(o.reward) +'"></td>' +
													   '<td><input class="but_set_state btn btn-sm btn-borB" type="submit" value="删除" durl="/business/financial/cashier?action=delproduct" did="'+ o.finance_id+'" mid="'+ o.member_id+'"/></td>' +
												   '</form>'+
										   		'</tr>';

										parent.$('.table tbody').append(html);


									});
									var allMoney = arrSum.reduce(function (x, y) {
										return x + y;
									});
									parent.$('.table .types').each(function(){
										if($(this).find('i').text() !=='储值卡'){
											$(this).find('span').hide();
										}
									});
									parent.$('.table tbody').append('<tr>' +
										'<td colspan="1" style="border-right: 0;">总计金额：' +
										'<em class="red">￥<span class="total-money">'+allMoney+'</span></em>' +
										'</td>' +
										'<td class="text-right" colspan="8" style="border-left: 0;">' +
										'<em class="red">提示：</em>' +
										'会员有多次服务消费的情况，请选择疗程卡；会员单次项目服务消费的情况，请选择有服务项目会员消费后，无需提供后续服务，请选择无服务项目' +
										'</td>' +
										'</tr>');
									parent.$('.table').parent().find('.clearingBtn').removeClass('hide');

									parent.layer.close(index);
								}else{
									layer.alert(data.message, {icon: 5});
								}
							}
						})
					}else{
						layer.alert(data.message, {icon: 5});
					}
				}
			})
		}

	}
	$(function(){
		window.formResult = formResult;
		window.checkparam = checkparam;
		$(".balancecard input[type='text']").dblclick(function(){
			if($("#edit").length > 0){
				layer.alert('请提交当前修改!',{icon:5});
				return;
			}
			$(this).attr('id','edit').attr('readonly',false);
			$(this).parent().find('.ok').css("display","inline-block");
			$(this).parent().parent().find('input[type="radio"]').attr("checked","checked");
			var val=$(this).parent().parent().find('input[type="radio"]').val();
			var $name = $(this).val();
			str = '<input type="text" name="card_id" value="'+val+'"><input type="hidden" name="consume_name" value="'+$name+'">';
			$('.hide-param').html(str);
		});
		$(".ok").click(function(){
			$(this).parent().find("input[name='title']").attr("id","").attr('readonly',true);
			$('.hide-param').find('input[name="consume_name"]').val($(this).parent().find('input[name="title"]').val());
			$(this).hide();

		});
		$(".addcard").click(function(){
			$(".newcard").show();
			$(".newcard input[type='text']").attr('id','edit').attr('readonly',false);
			$(this).attr('disabled','disabled');
		})
		$(".balancecard input[type='radio']").click(function(){
			var val = $(this).val();
			var $name = $(this).parent().find('input[name="title"]').val();
			str = '<input type="hidden" name="card_id" value="'+val+'"><input type="hidden" name="consume_name" value="'+$name+'">';
			$('.hide-param').html(str);
		})
	});
})(jQuery)

